# coding:utf-8
'''
	辅助工具类
	汉中库得网络科技
	电子信箱：yyc1030@qq.com
'''
from pymxs import runtime as rt

# 九宫格工具类
class NinePlaceGrid():
	def __init__(self):
		self.visible = False
		
	# 开启或关闭九宫格
	def toggle(self):
		if self.visible:
			self.visible = False
		else:
			self.visible = True
		rt.redrawViews()
			
	# 画九宫格，仅当显示安全框和九宫格开关打开时才会绘制
	def drawNinePlaceGrid(self):
		if rt.displaySafeFrames and self.visible:
			# 获取视口的长宽及宽高比
			width = rt.gw.getWinSizeX()
			height = rt.gw.getWinSizeY()
			viewportRatio = float(width) / float(height)
			renderRatio = rt.rendImageAspectRatio
			# 画横线
			rt.gw.setColor(rt.name("line"), rt.yellow)
			if viewportRatio <= renderRatio:
				# 当视口宽高比小于等于渲染比例时，宽度最大
				# 计算安全框高度
				sfH = width / renderRatio
				# 计算顶部空白区域高度
				noneH = (height - sfH) / 2
				# 画横线
				line1H = noneH + sfH / 3	
				line2H = noneH + sfH / 3 * 2
				rt.gw.wPolyline([rt.point3(0, line1H, 0), rt.point3(width, line1H, 0)], False)
				rt.gw.wPolyline([rt.point3(0, line2H, 0), rt.point3(width, line2H, 0)], False)
				# 画竖线
				line1W = width / 3
				line2W = width / 3 * 2
				rt.gw.wPolyline([rt.point3(line1W, noneH, 0), rt.point3(line1W, noneH + sfH, 0)], False)
				rt.gw.wPolyline([rt.point3(line2W, noneH, 0), rt.point3(line2W, noneH + sfH, 0)], False)
			else:
				# 当视口宽高比大于渲染比例时，高度最大
				# 计算安全框宽度
				sfW = height * renderRatio
				# 计算安全框左侧空白区域的宽度
				noneW = (width - sfW) / 2
				# 画横线
				line1H = height / 3
				line2H = height / 3 * 2
				rt.gw.wPolyline([rt.point3(noneW, line1H, 0), rt.point3(noneW + sfW, line1H, 0)], False)
				rt.gw.wPolyline([rt.point3(noneW, line2H, 0), rt.point3(noneW + sfW, line2H, 0)], False)
				# 画竖线
				line1W = noneW + sfW / 3
				line2W = noneW + sfW / 3 * 2
				rt.gw.wPolyline([rt.point3(line1W, 0, 0), rt.point3(line1W, height, 0)], False)
				rt.gw.wPolyline([rt.point3(line2W, 0, 0), rt.point3(line2W, height, 0)], False)
			# 打印测试信息
			#rt.gw.wText(rt.point3(10, 40, 0), "测试信息：", color=rt.yellow)
			#rt.gw.wText(rt.point3(10, 60, 0), "屏幕尺寸：%s, %s 比例：%s" % (width, height, viewportRatio), color=rt.yellow)
			#rt.gw.wText(rt.point3(10, 80, 0), "渲染比例：%s" % renderRatio, color=rt.yellow)
	
	
# 相机工具类
class Camera():
	def __init__(self):
		# 当视口变化时
		# 生成MaxScript 钩子
		rt.callbacks.removeScripts(rt.name("viewportChange"), id=rt.name("updateRenderSize"))
		rt.execute("fn updateRenderSize = ()")
		rt.updateRenderSize = self.updateRenderSize
		rt.callbacks.addScript(rt.name("viewportChange"), "updateRenderSize()", id=rt.name("updateRenderSize"))
		
	# 选中活动视口的相机
	def selectCurrentCamera(self):
		camera = rt.getActiveCamera()
		if camera:
			rt.select(camera)
			
	# 记录相机纵横比
	def markCurrentCamera(self):
		# 如果渲染设置对话框是打开的，强制提交修改后的值
		if rt.renderSceneDialog.isOpen():
			rt.renderSceneDialog.commit()
		camera = rt.getActiveCamera()
		if camera:
			rt.setUserPropVal(camera, "width", rt.renderWidth)
			rt.setUserPropVal(camera, "height", rt.renderHeight)
			rt.setUserPropVal(camera, "ratio", rt.rendImageAspectRatio)
		rt.redrawViews()
			
	# 在活动视口画出相机记录的值
	def drawCameraMarks(self):
		camera = rt.getActiveCamera()
		if camera:
			if rt.getUserPropVal(camera, "width"):
				rt.gw.wText(rt.point3(10, 40, 0), "宽    度：%s\n高    度：%s\n纵横比：%0.3f" % (rt.getUserPropVal(camera, "width"), rt.getUserPropVal(camera, "height"), rt.getUserPropVal(camera, "ratio")), color=rt.yellow)
			else:
				rt.gw.wText(rt.point3(10, 40, 0), "未记录", color=rt.yellow)
		
	# 更新渲染尺寸
	def updateRenderSize(self):
		camera = rt.getActiveCamera()
		if camera:
			rt.renderWidth = rt.getUserPropVal(camera, "width")
			rt.renderHeight = rt.getUserPropVal(camera, "height")
			rt.rendImageAspectRatio = rt.getUserPropVal(camera, "ratio")